<?php
class Admin_OrderController extends Zend_Controller_Action{
	protected $_model = null;
	
	public function init(){
		$this->_model = new Application_Model_DbTable_Order();
	}
	
	public function indexAction(){
		$filters = array();
		
		//từ khóa
		$keyword = $this->_request->getParam('keyword','');
		$filters['keyword']=$keyword;
		$this->view->keyword = $keyword;
	
		
		// từ ngày
		$fromDate = $this->_request->getParam('fromDate',null);
		//$filters['fromDate']=Cms_String_String::getIntFromDate($fromDate);
		$filters['fromDate']=Cms_String_String::getDateFromString($fromDate);
		$this->view->fromDate = $fromDate;
		
		// đến ngày
		$toDate = $this->_request->getParam('toDate',null);
		$filters['toDate']=Cms_String_String::getDateFromString($toDate,'last');//chuyển về định dạng của mysql
		$this->view->toDate = $toDate;
		
		// trạng thái | status
		$status = $this->_request->getParam('searchStatus',-1);
		$filters['status']=$status;
		$this->view->status=$status;
		
		// sắp xếp | order
		$order = $this->_request->getParam('order',"");
		if($order!=""){
			list($orderBy,$orderType) = explode('-', $order);
		}else{
			$orderBy=null;
			$orderType=null;
		}
		
		
		//get item count per page
		$icpp = $this->_request->getParam('icpp',20);
		
		//get current page number
		$currentPage = $this->_request->getParam('page',1);
		
		$list = $this->_model->getList($filters,$orderType,$orderBy);
	
		
		// pagination
		$paginator = Zend_Paginator::factory($list);
		$paginator->setCurrentPageNumber($currentPage);
		$paginator->setItemCountPerPage($icpp);
		$paginator->setPageRange(5);
		
		//assign to view
		$this->view->paginator=$paginator;
	}
	
	public function insertAction(){
		
	}
	
	public function updateAction(){
		$id = $this->_request->getParam('id');
		$orderInfo = $this->_model->find($id)->current();
		$this->view->orderInfo = $orderInfo;
		
		//thông tin khách hàng
		$mdUser = new Application_Model_DbTable_User();
		$userInfo = $mdUser->find($orderInfo->order_user_id)->current();
		$this->view->userInfo = $userInfo;
		
		//chi tiết đơn hàng
		$mdOrderDetail = new Application_Model_DbTable_OrderDetail();
		
		$this->view->orderdetail = $mdOrderDetail->fetchAll('order_detail_order_code = '.$orderInfo->order_code);
		if($this->_request->isPost()){
			$orderInfo->order_address = $this->_request->getParam('order_address');
			$orderInfo->order_note = $this->_request->getParam('order_note');
			$orderInfo->order_status = $this->_request->getParam('order_status');
			$orderInfo->save();
            
			$this->_helper->flashMessenger('Cập nhật đơn hàng thành công!');
			$this->_helper->redirector('index','order','admin');
		}
	
	}
	
	public function deleteAction(){
		$status = 0;
		$id = @(array)$this->_request->getParam('id');
		$mdOrderDetail = new Application_Model_DbTable_OrderDetail();
		$rows = $this->_model->fetchAll($this->_model->select()->where('order_id IN (?)',$id));
		if(count($rows)>0){
			foreach($rows as $row):	
				try {
					//xóa ở order detail table					
					$mdOrderDetail->delete('order_detail_order_code = '.$row->order_code);    
                                   								
					//xóa ơr order table
					$row->delete();
					$status = 1;					
					
				} catch (Exception $e) {
					$status = 0;
					$this->_helper->flashMessenger('Xóa dữ liệu thất bại! '. $e->getMessage());
					$this->_helper->redirector('index','order','admin');
				}
			endforeach;
			$this->_helper->flashMessenger('Xóa dữ liệu thành công!');
			$this->_helper->redirector('index','order','admin');
		}
	}
	
	public function viewAction(){
		$id = $this->_request->getParam('id');
		$orderInfo = $this->_model->find($id)->current();
		$this->view->orderInfo = $orderInfo;
		
		//thông tin khách hàng
		$mdUser = new Application_Model_DbTable_User();
		$userInfo = $mdUser->find($orderInfo->order_user_id)->current();
		$this->view->userInfo = $userInfo;
		
		//chi tiết đơn hàng
		$mdOrderDetail = new Application_Model_DbTable_OrderDetail();
		
		$this->view->orderdetail = $mdOrderDetail->fetchAll('order_detail_order_code = '.$orderInfo->order_code);
	}
}













